// $Id$
package cz.pd.projectbuilder.main.util;

import com.thoughtworks.xstream.XStream;
import cz.pd.projectbuilder.beans.load.ActionBean;
import cz.pd.projectbuilder.beans.load.ConfigBean;
import cz.pd.projectbuilder.beans.load.ProjectBean;
import cz.pd.projectbuilder.beans.load.ProjectBuilderBean;
import cz.pd.projectbuilder.beans.load.ProjectGroupBean;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author diblik
 * @version $Revision$
 */
public class XmlUtils {

//  public static final String PROJECT_CONFIG_FILE_XML = "src/main/resources/Projects.xml";
//  public static final String PROJECT_SAVED_FILE_XML = "src/main/resources/SavedProjects.xml";
  private static final String PROJECT_CONFIG_FILE_XML = "conf/Projects.xml";
  private static final String PROJECT_SAVED_FILE_XML = "conf/saved/SavedProjects.xml";
  private static ProjectBuilderBean loadedProjectBuilderBean;

  public static ProjectBuilderBean loadMainProjectBuilder() {

    if (loadedProjectBuilderBean != null) {
      return loadedProjectBuilderBean;
    }
    
    try {
      XStream xstream = new XStream();
      xstream.processAnnotations(ProjectBuilderBean.class);
      System.out.println("78: "
              + XmlUtils.class.getClassLoader().getResource(PROJECT_CONFIG_FILE_XML));
      loadedProjectBuilderBean = (ProjectBuilderBean) xstream.fromXML(new FileReader(PROJECT_CONFIG_FILE_XML));
      return loadedProjectBuilderBean;
//    return (ProjectBuilderBean) xstream.fromXML(XmlUtils.class.getClassLoader().getResourceAsStream(
//        PROJECT_CONFIG_FILE_XML));
    } catch (FileNotFoundException ex) {
      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
//    return (ProjectBuilderBean) xstream.fromXML(XmlUtils.class.getClassLoader().getResourceAsStream(
//        PROJECT_CONFIG_FILE_XML));
  }

  public static void saveProjectBuilder(ProjectBuilderBean projectBuilderBean) {
    try {
      XStream xstream = new XStream();
      xstream.processAnnotations(ProjectBuilderBean.class);
      FileWriter fileWriter = new FileWriter(PROJECT_SAVED_FILE_XML);
      fileWriter.write(xstream.toXML(projectBuilderBean));
      fileWriter.close();
    } catch (IOException ex) {
      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public static ProjectBuilderBean loadSavedProjectBuilder() {
    try {
      XStream xstream = new XStream();
      xstream.processAnnotations(ProjectBuilderBean.class);
      return (ProjectBuilderBean) xstream.fromXML(new FileReader(PROJECT_SAVED_FILE_XML));
    } catch (FileNotFoundException ex) {
      Logger.getLogger(XmlUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
  }

  public static void test() {
//    try {
    XStream xstream = new XStream();
    xstream.processAnnotations(ProjectBuilderBean.class);
//    xstream.setMode(XStream.ID_REFERENCES);
//    xstream.alias("akce", ActionBean.class);
//    xstream.alias("projekt", ProjectBean.class);
//    xstream.alias("projektBuilder", ProjectBuilderBean.class);
//
    xstream.aliasAttribute("akce", "akce");
    ArrayList<ActionBean> actionBeans = new ArrayList<ActionBean>();
    actionBeans.add(new ActionBean("button1", "action1"));
    actionBeans.add(new ActionBean("button2", "action2"));

    ArrayList<ProjectBean> projectBeans = new ArrayList<ProjectBean>();
    projectBeans.add(new ProjectBean("test1", "cesta1"));
    projectBeans.add(new ProjectBean("test2", "cesta2"));

    ArrayList<ProjectBean> projectBeans2 = new ArrayList<ProjectBean>();
    projectBeans2.add(new ProjectBean("test3", "cesta3"));
    projectBeans2.add(new ProjectBean("test4", "cesta4"));

    ArrayList<ProjectGroupBean> projectGroupBeans = new ArrayList<ProjectGroupBean>();
    projectGroupBeans.add(new ProjectGroupBean("CORE", "cestaZmesta", projectBeans));
    projectGroupBeans.add(new ProjectGroupBean("CMP", "cestaDomesta", projectBeans2));

    ArrayList<ProjectBean> projectBeans3 = new ArrayList<ProjectBean>();
    projectBeans3.add(new ProjectBean("test1", "cesta1"));
    projectBeans3.add(new ProjectBean("test2", "cesta2"));


    ArrayList<ProjectGroupBean> suiteGroupBeans = new ArrayList<ProjectGroupBean>();
    suiteGroupBeans.add(new ProjectGroupBean("CORE14", "cestaZmesta14", projectBeans3));

    ConfigBean configBean = new ConfigBean();
    configBean.setRunSuite(true);
//
    String data = xstream.toXML(new ProjectBuilderBean(projectGroupBeans, actionBeans, suiteGroupBeans, configBean));
    System.out.println(data);
//      ProjectBuilderBean fromXML =
//          (ProjectBuilderBean) xstream.fromXML(new FileReader("src/main/resources/Projects.xml"));
//      System.out.println(fromXML);
//    } catch (FileNotFoundException ex) {
//      Logger.getLogger(XmlUtil.class.getName()).log(Level.SEVERE, null, ex);
//    }
  }
}
